// ------------- Steps -------------------
// 1. Create Cursive Instance
// 2. Conf Cursive Instance
// 3. Exec Cursive  Instance

// use cursive::views::{Dialog, TextView};// for output text
// fn main() {
//     let mut cur = cursive::default();
//     cur.add_global_callback('q', |s| s.quit());
//     cur.add_layer(TextView::new("Hello Cursive, press q to quit."));
//     cur.add_layer(Dialog::around(TextView::new("Question 1")));
//     cur.add_layer(Dialog::text("Question 2").title("For helper or FAQ"));
//     cur.add_layer(Dialog::text("Can I get your message?").title("Dialog with me?")
//                         .button("Yes", |s| {
//                             s.pop_layer();// remove current dialog layer
//                         }
//                         )
//                         .button("No", |s| {
//                             s.pop_layer();
//                             s.add_layer(TextView::new("are you sure for no?"));
//                         })
//                         .button("Not Reachable", |s| s.quit())
//                   );
//     cur.run();
// }
use cursive::CursiveExt;

use cursive::Cursive;
use cursive::traits::*;
use cursive::views::{Dialog, DummyView, LinearLayout, SelectView, TextView};

use std::fs::{self, DirEntry, File};
use std::io::Read;
use std::path::Path;

fn main() {
    let mut app = Cursive::new();
    let mut panes = LinearLayout::horizontal();
    let picker = file_picker(".");
    panes.add_child(picker.fixed_size((30, 25)));
    panes.add_child(DummyView);
    panes.add_child(TextView::new("file contents")
        .with_id("contents")
        .fixed_size((50, 25)));
    let mut layout = LinearLayout::vertical();
    layout.add_child(panes);
    layout.add_child(TextView::new("status")
        .scrollable(false)
        .with_id("status")
        .fixed_size((80, 1)));
    app.add_layer(Dialog::around(layout).button("Quit", |a| a.quit()));
    app.run();
}
fn file_picker<D>(directory: D) -> SelectView<DirEntry>
    where D: AsRef<Path>
{
    let mut view = SelectView::new();
    for entry in fs::read_dir(directory).expect("can't read directory") {
        if let Ok(e) = entry {
            let file_name = e.file_name().into_string().unwrap();
            view.add_item(file_name, e);
        }
    }
    view.on_select(update_status).on_submit(load_contents)
}fn update_status(app: &mut Cursive, entry: &DirEntry) {
    let status_bar = app.find_id::<TextView>("status").unwrap();
    let file_name = entry.file_name().into_string().unwrap();
    let file_size = entry.metadata().unwrap().len();
    let content = format!("{}: {} bytes", file_name, file_size);
    status_bar.set_content(content);
}

fn load_contents(app: &mut Cursive, entry: &DirEntry) {
    let text_view = app.find_id::<TextView>("contents").unwrap();
    let content = if entry.metadata().unwrap().is_dir() {
        "<DIR>".to_string()
    } else {
        let mut buf = String::new();
        let _ = File::open(entry.file_name())
            .and_then(|mut f| f.read_to_string(&mut buf))
            .map_err(|e| buf = format!("Error: {}", e));
        buf
    };
    text_view.set_content(content);
}